<?php
namespace app\api\controller;
use app\common\BaseController;
use think\Db;
use app\api\model\Receipt as ReceiptModel;
use app\api\model\Payment as PaymentModel;
use app\api\model\Transfer as TransferModel;
use app\common\Logs;
class ThreeCol extends BaseController{    
    public function __construct(){
        parent::__construct();       
    }
    public function index(){   
        $where = [];
        $rwhere=[];
        $pwhere=[];
        $twhere=[];
        $fwhere=[];
        $params = $this->request->param();       
        $page = 1;
        $pageCount = 10;        
        $datas=[];
        if(!empty($params)){      
             //按收付款日期查询
             if(!empty($params['start_date']) && !empty($params['end_date'])){
                $rwhere['receipt_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];
                $pwhere['payment_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];
                $twhere['transfer_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];
                $fwhere['transfer_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];
            }else{
                if(!empty($params['start_date'])){
                    $rwhere['receipt_date'] = ['>= ',$params['start_date']];  
                    $pwhere['payment_date'] = ['>= ',$params['start_date']];  
                    $twhere['transfer_date'] = ['>= ',$params['start_date']];  
                    $fwhere['transfer_date'] = ['>= ',$params['start_date']];  
                }
                if(!empty($params['end_date'])){
                    $rwhere['receipt_date'] = ['<=',$params['end_date']];  
                    $pwhere['payment_date'] = ['<=',$params['end_date']];  
                    $twhere['transfer_date'] = ['<=',$params['end_date']];  
                    $fwhere['transfer_date'] = ['<=',$params['end_date']];  
                }
            }     
           
            //页码            
            $page = !empty($params['page']) ? $params['page'] : $page;
            $pageCount = !empty($params['page_count']) ? $params['page_count'] : $pageCount;
            //按客户查询
            if(!empty($params['client_id'])){
                $rwhere['client_id'] = $params['client_id'];        
            }           
            //按供应商查询
            if(!empty($params['supplier_id'])){
                $pwhere['supplier_id'] = $params['supplier_id'];             
            }   
            //按科目查询
            if(!empty($params['subject_id'])){
                $rwhere['subject_id'] = $params['subject_id'];    
                $pwhere['subject_id'] = $params['subject_id'];        
                $twhere['to_bank'] = $params['subject_id']; 
                $fwhere['from_bank'] = $params['subject_id'];        
            }   
            //按科目查询
            if(!empty($params['invoice_num'])){
                $rwhere['invoice_num'] = $params['invoice_num'];    
                $pwhere['invoice_num'] = $params['invoice_num'];        
                $twhere['to_number'] = $params['invoice_num']; 
                $fwhere['from_number'] = $params['invoice_num'];        
            }     
            //按科目查询
            if(!empty($params['sn'])){
                $rwhere['sn'] = $params['sn'];    
                $pwhere['sn'] = $params['sn'];        
                $twhere['sn'] = $params['sn']; 
                $fwhere['sn'] = $params['sn'];        
            }      
        }  
        $Payment = new PaymentModel();
       $Receipt = new ReceiptModel();
        $Transfer = new TransferModel();
        $a = $Receipt->alias('a')
        ->field("a.id,a.type,a.is_review,a.in,a.receipt_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.client_id as x_id,b.guest_id,c.start_date,c.sn,c.title")
        ->join('lt_team_income b',['a.income_id=b.id','a.type=b.type'])
        ->join('lt_team c','b.parent_id=c.id')
        ->where($rwhere)
        ->buildSql();               
        $b = $Receipt->alias('a')
        ->field("a.id,a.type,a.is_review,a.in,a.receipt_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.client_id as x_id,b.guest_id,b.start_date,b.sn,b.product_name as title")
        ->join('lt_single b',['a.income_id=b.id','a.type=b.type'])
        ->where($rwhere)
        ->buildSql();             
        $c = $Payment->alias('a')
        ->field("a.id,a.type,a.is_review,a.in,a.payment_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.supplier_id as x_id,b.supplier_id as guest_id,c.start_date,c.sn,c.title")
        ->join('lt_team_outlay b',['a.outlay_id=b.id','a.type=b.type'])
        ->join('lt_team c','b.parent_id=c.id')
        ->where($pwhere)        
        ->buildSql();     
        $d = $Payment->alias('a')
        ->field("a.id,a.type,a.is_review,a.in,a.payment_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.supplier_id as x_id,b.supplier_id as guest_id,b.start_date,b.sn,b.product_name as title")
        ->join('lt_single b',['a.outlay_id=b.id','a.type=b.type'])       
        ->where($pwhere)      
        ->buildSql();  
        $e = $Transfer->alias('a')
        ->field("a.id,a.type,a.is_to_review as is_review,a.to_in,a.transfer_date as price_date,a.to_bank as subject_id,a.to_money as money,a.to_number as invoice_num,a.remark,a.x_id,a.x_id as guest_id,a.start_date,a.sn,a.title")        
        ->where($twhere)      
        ->buildSql();    

        $f = $Transfer->alias('a')
        ->field("a.id,a.type,a.is_from_review as is_review,a.from_in,a.transfer_date as price_date,a.from_bank as subject_id,a.from_money as money,a.from_number as invoice_num,a.remark,a.x_id,a.x_id as guest_id,a.start_date,a.sn,a.title")        
        ->where($fwhere)      
        ->buildSql();  

        if(!empty($params['client_id'])){
            $datas = Db::table($a.' a')->union([$b])->orderRaw("CAST(invoice_num as decimal)")->select();
        }elseif(!empty($params['supplier_id'])){
            $datas = Db::table($c.' c')->union([$d])->orderRaw("CAST(invoice_num as decimal)")->select();
        }else{
            $datas = Db::table($a.' a')->union([$b,$c,$d,$e,$f])->orderRaw("CAST(invoice_num as decimal)")->select();          
        }     
        
        foreach ($datas as &$item) {
            if($item['in']==1){
                $item['guest_name']=Db::name("guest")->where("id",$item['guest_id'])->value('guest_name');
                $item['client_name']=Db::name("client")->where("id",$item['x_id'])->value('company_name');
            }else{
                $item['supplier_name']=Db::name("supplier")->where("id",$item['x_id'])->value('supplier_name');
            }
        }
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => 0
        );
        return $datalist;     
    }   

    public function yuer(){   
        $where = [];
        $rwhere=[];
        $pwhere=[];
        $params = $this->request->param(); 
        $datas=[];
        if(!empty($params)){      
             //按收付款日期查询            
            if(!empty($params['start_date'])){
                $rwhere['receipt_date'] = ['<',$params['start_date']];  
                $pwhere['payment_date'] = ['<',$params['start_date']];  
                $cwhere['transfer_date'] = ['<',$params['start_date']];  
                $dwhere['transfer_date'] = ['<',$params['start_date']];  
            }  
            //按科目查询
            if(!empty($params['subject_id'])){
                $rwhere['subject_id'] = $params['subject_id'];      
                $pwhere['subject_id'] = $params['subject_id'];          
                $cwhere['to_bank'] = $params['subject_id'];  
                $dwhere['from_bank'] = $params['subject_id'];        
            }         
        }  
       
        $a = Db::name("Receipt")->where($rwhere)->sum('money');
        $b = Db::name("Payment")->where($pwhere)->sum('money');
        $c = Db::name("transfer")->where($cwhere)->sum('to_money');
        $d = Db::name("transfer")->where($dwhere)->sum('from_money');
        $e = Db::name("bank")->where('id',$params['subject_id'])->value('balance');
        $y=$e+$a+$c-$b-$d;      
       
        return $y;     
    }   

    public function review(){
        $params = json_decode(file_get_contents('php://input'),true);
       
        $count = 0;
        foreach ($params['list'] as $key => $item) {            
            if($item['in']==="1"){
                //收入
                if($item['type']==="11"){
                    $old=Db::name("transfer")->where('id',$item['id'])->find();       
                    $data=Db::name("transfer")->where('id',$item['id'])->update(['is_to_review'=>'1']);  
                    if($data){    
                        $data=Db::name("transfer")->where('id',$item['id'])->find();                        
                        Logs::add("审核收款记录",$item['operator_id'],$data["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($data,JSON_UNESCAPED_UNICODE));       
                    }
                }else{
                    $old=Db::name("receipt")->where('id',$item['id'])->find();       
                    $data=Db::name("receipt")->where('id',$item['id'])->update(['is_review'=>'1']); 
                    if($data){  
                        $data=Db::name("receipt")->where('id',$item['id'])->find();                      
                        if(in_array($item['type'], [1,2,3,10])){                                    
                            $parent = Db::name("team_income")->alias('a')->join('lt_team b','a.parent_id=b.id')->where('a.id',$old['income_id'])->find();
                         }else{
                            $parent = Db::name("single")->where('id',$old['income_id'])->find();
                        }                      
                        Logs::add("审核收款记录",$item['operator_id'],$parent["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($data,JSON_UNESCAPED_UNICODE));       
                    } 
                }                
            }else{
                //支出
                if($item['type']==="11"){
                    $old=Db::name("transfer")->where('id',$item['id'])->find();       
                    $data=Db::name("transfer")->where('id',$item['id'])->update(['is_from_review'=>'1']);  
                    if($data){    
                        $data=Db::name("transfer")->where('id',$item['id'])->find();                        
                        Logs::add("审核付款记录",$item['operator_id'],$data["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($data,JSON_UNESCAPED_UNICODE));       
                    }
                }else{
                    $old=Db::name("payment")->where('id',$item['id'])->find();       
                    $data=Db::name("payment")->where('id',$item['id'])->update(['is_review'=>'1']); 
                    if($data){    
                        $data=Db::name("payment")->where('id',$item['id'])->find();
                        if(in_array($item['type'], [1,2,3,10])){                            
                            $parent = Db::name("team_outlay")->alias('a')->join('lt_team b','a.parent_id=b.id')->where('a.id',$old['outlay_id'])->find();
                         }else{
                            $parent = Db::name("single")->where('id',$old['outlay_id'])->find();
                        }                      
                        Logs::add("审核付款记录",$item['operator_id'],$parent["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($data,JSON_UNESCAPED_UNICODE));       
                    } 
                }   
            }
            if($data){
                $count++;
            }            
        }
        return $count;
    }   
    //取消审核
    public function cancel_review(){
        $params = json_decode(file_get_contents('php://input'),true);
        $count = 0;
        foreach ($params['list'] as $key => $item) {            
            if($item['in']==="1"){
                //收入
                if($item['type']==="11"){
                    $old=Db::name("transfer")->where('id',$item['id'])->find();       
                    $data=Db::name("transfer")->where('id',$item['id'])->update(['is_to_review'=>'0']);  
                    if($data){    
                        $data=Db::name("transfer")->where('id',$item['id'])->find();                        
                        Logs::add("解除审核收款记录",$item['operator_id'],$data["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($data,JSON_UNESCAPED_UNICODE));       
                    }
                }else{
                    $old=Db::name("receipt")->where('id',$item['id'])->find();       
                    $data=Db::name("receipt")->where('id',$item['id'])->update(['is_review'=>'0']); 
                    if($data){ 
                        $data=Db::name("receipt")->where('id',$item['id'])->find();      
                        if(in_array($item['type'], [1,2,3,10])){                            
                            $parent = Db::name("team_income")->alias('a')->join('lt_team b','a.parent_id=b.id')->where('a.id',$old['income_id'])->find();
                         }else{
                            $parent = Db::name("single")->where('id',$old['income_id'])->find();
                        }                      
                        Logs::add("解除审核收款记录",$item['operator_id'],$parent["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($data,JSON_UNESCAPED_UNICODE));       
                    } 
                }                
            }else{
                //支出
                if($item['type']==="11"){
                    $old=Db::name("transfer")->where('id',$item['id'])->find();       
                    $data=Db::name("transfer")->where('id',$item['id'])->update(['is_from_review'=>'0']);  
                    if($data){    
                        $data=Db::name("transfer")->where('id',$item['id'])->find();                        
                        Logs::add("解除审核付款记录",$item['operator_id'],$data["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($data,JSON_UNESCAPED_UNICODE));       
                    }
                }else{
                    $old=Db::name("payment")->where('id',$item['id'])->find();       
                    $data=Db::name("payment")->where('id',$item['id'])->update(['is_review'=>'0']); 
                    if($data){ 
                        $data=Db::name("payment")->where('id',$item['id'])->find();          
                        if(in_array($item['type'], [1,2,3,10])){                            
                            $parent = Db::name("team_outlay")->alias('a')->join('lt_team b','a.parent_id=b.id')->where('a.id',$old['outlay_id'])->find();
                         }else{
                            $parent = Db::name("single")->where('id',$old['outlay_id'])->find();
                        }                      
                        Logs::add("解除审核付款记录",$item['operator_id'],$parent["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($data,JSON_UNESCAPED_UNICODE));       
                    } 
                }   
            }
            if($data){
                $count++;
            }            
        }
        return $count;
    }    
}
